package cleancode.model;

import java.util.Arrays;

/**
 * Model for operation.
 */
public class Operation extends NameBasedModel
{
    private static final String[] ALWAYS_ALLOWED_OPERATIONS = new String[]{ "help", "switchUser" };

    private static final String[] DATA_MODIFY_OPERATIONS = new String[]{ "listUser", "listRole", "listOperation",
                                                                        "addUser", "addRole", "addOperation",
                                                                        "removeUser", "removeRole", "removeOperation",
                                                                        "addRoleTo", "addOperationTo",
                                                                        "removeRoleFrom", "removeOperationFrom" };

    public static NameOrientedModelSet<Operation> getBasicOperations()
    {
        NameOrientedModelSet<Operation> basicOperationSet = new NameOrientedModelSet<>();
        for( String name : ALWAYS_ALLOWED_OPERATIONS )
        {
            basicOperationSet.add( new Operation( name ) );
        }
        for( String name : DATA_MODIFY_OPERATIONS )
        {
            basicOperationSet.add( new Operation( name ) );
        }
        return basicOperationSet;
    }

    public Operation( String name )
    {
        super( name );
    }

    public boolean isAwaysAllowed()
    {
        return isArrayElement( ALWAYS_ALLOWED_OPERATIONS );
    }

    public boolean isDataModifyOperation()
    {
        return isArrayElement( DATA_MODIFY_OPERATIONS );
    }

    public boolean isBasicOperation()
    {
        return isAwaysAllowed() || isDataModifyOperation();
    }

    private boolean isArrayElement( String[] operationArray )
    {
        return Arrays.asList( operationArray ).contains( getName() );
    }

}
